package com.wuseguang.springcloud.controller;

import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.entity.ClassDutyList;
import com.wuseguang.springcloud.service.ClassDutyService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @描述 数据管理-->值日管理
 * @创建人 zm
 * @创建时间 2020/1/22
 */
@RestController
@RequestMapping("/classDuty")
public class ClassDutyController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ClassDutyService classDutyService;

    @Autowired
    private ClassCardService classCardService;

    /**
     * @描述 查询所有值日信息
     * @创建人 zm
     * @创建时间 2020/1/22
     */
    @RequestMapping("/queryClassDutyAll")
    public Result queryClassDutyAll(RequestParameterWrapper request,
                                    @RequestParam(value = "classid") Integer classid) {
        String schoolId = request.getParam("schoolId");
        logger.info("queryClassDutyAll==>schoolId={},classid={}", schoolId, classid);
        return new Result(classDutyService.queryClassDutyAll(Integer.valueOf(schoolId), classid));
    }


    /**
     * @描述 批量添加值日信息
     * @创建人 zm
     * @创建时间 2020/1/22
     */
    @RequestMapping("/addClassDuty")
    public Result addClassDuty(RequestParameterWrapper request,
                               @RequestBody ClassDutyList classDutyList) {
        if (classDutyList == null || classDutyList.getStarttime() == null) {
            return new Result(CodeUtil.ADD_FAILURE);
        }
        logger.info("addClassDuty==>classDutyList={}", classDutyList.toString());
        String schoolId = request.getParam("schoolId");
        Result result = classDutyService.addClassDuty(Integer.valueOf(schoolId), classDutyList);
        classCardService.classesAndCards(classDutyList.getClassid());
        return result;
    }

    /**
     * @描述 根据班级查学生
     * @创建人 zm
     * @创建时间 2020/3/15
     */
    @RequestMapping("/findStudentByClassId")
    public Result findStudentByClassId(@RequestParam(value = "classid") Integer classid) {
        return new Result(classDutyService.findStudentByClassId(classid));
    }


    /**
     * @描述 自动导入
     * @创建人 zm
     * @创建时间 2020/3/15
     */
    @RequestMapping("/autoImportDuty")
    public Result autoImportDuty(@RequestParam(value = "classId") Integer classId,
                                 @RequestParam(value = "x") Integer x,
                                 @RequestParam(value = "y") Integer y) {
        return classDutyService.autoImportDuty(classId, x, y);
    }

    /**
     * @描述 导出值日信息
     * @创建人 zm
     * @创建时间 2020/3/15
     */
    @RequestMapping("/exportDuty")
    public void exportDuty(RequestParameterWrapper request, HttpServletResponse response, @RequestParam(value = "classid") Integer classId) {
        logger.info("导出班级值日信息");
        classDutyService.exportDuty(request, response, classId);
    }
}
